package com.bdmc.smokesensor.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bdmc.smokesensor.dao.Sys_UserJPA;
import com.bdmc.smokesensor.entity.Sys_User;
import com.bdmc.smokesensor.iservices.ISys_UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

@Service
public class Sys_UserService extends BaseService<Sys_User> implements ISys_UserService {

    @Autowired
    private Sys_UserJPA _userjpa;

    @Override
    public List<Sys_User> findAll() {

        return _userjpa.findAll();
    }

    @Override
    public Map<String, Object> findBySqlPageable(int dep_id, String name, int page, int rows) {
        //Page<Map<String, Object>> p = _userjpa.findAllByTreeRoot(dep_id, dep_id + ",", PageRequest.of(page, rows));
        String sql = "select u.*,d.depName,r.roleName,mtc.mainName from sys_user u left join rbac_role r on r.id = u.rbac_RoleId"
            +" left join smk_maintenance mtc on mtc.id=u.smk_MaintenanceId  "
                + " left join sys_department d on u.departmentId=d.id" + " where (d.id=" + dep_id
                + " or d.treeRoot like '%," + dep_id + ",%') and u.nickname like '%" + name + "%' order by u.id desc";
        List<Map<String, Object>> data = getListMapBySql(sql, page, rows);
        int t = GetCountBySql(sql);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", t);
        map.put("data", data);
        return map;
    }

    @Override
    public Sys_User findOne(Example<Sys_User> example) {
        return _userjpa.findOne(example).orElse(null);
    }

    @Override
    public Sys_User findOneById(int id) {
        return _userjpa.findById(id).orElse(null);
    }

    @Override
    public Sys_User save(Sys_User user) {
        return _userjpa.save(user);
    }

    @Override
    public void deleteById(int id) {
        _userjpa.deleteById(id);
    }

    @Override
    public Sys_User findByLoginNameAndPassword(String loginName, String password) {
        return _userjpa.findByLoginNameAndPassword(loginName, password);
    }

    @Override
    public List<String> findByDepartmentIdIn(Integer d_id,List<Integer> ids) {
        return _userjpa.findByDepartmentIdIn(d_id,ids);
    }

    @Override
    public List<Sys_User> findPushByDepartmentIdIn(Integer d_id,List<Integer> ids) {
        return _userjpa.findPushByDepartmentIdIn(d_id,ids);
    }

    @Override
    public Sys_User findByOpenid(String openid) {
        return _userjpa.findByOpenid(openid);
    }

    @Override
    public Sys_User findByOpenidAndRoleNum(String openid, String roleNum) {
        return _userjpa.findByOpenidAndRoleNum(openid, roleNum);
    }
}